package com.iceengine.j3d.models;

/**
 * This class can load the IceModel format.
 * @author penguin
 */
import com.iceengine.j3d.objects.*;
import java.io.*;
import org.lwjgl.util.vector.*;
import com.iceengine.j3d.helper.Loader;
import org.newdawn.slick.opengl.Texture;

public class IceModel {

    SimpleObject theObject;

    /**
     * Creates the WorldObject where the model will be drawn in.
     * @param Translation Object translation.
     */
    public IceModel(Vector3f Translation)
    {
        theObject = new SimpleObject(Translation);
    }

    /**
     * Load a model that is located at the given path.
     * @param Path Path where the model is located.
     */
    public void loadModel(String Path)
    {
        try
        {
            BufferedReader theReader = new BufferedReader(new FileReader(Path));
            String currentLine = "";
            IFigure currentFigure = null;
            while((currentLine = theReader.readLine()) != null)
            {
                String[] currentCommands = currentLine.split(" ");
                switch(currentCommands[0].hashCode())
                {
                    case 109267: // not
                        String TextureName = currentCommands[2];
                        String TextureDecoder = currentCommands[3];
                        Texture myTexture = Loader.loadTexture(TextureDecoder, TextureName);
                        switch (currentCommands[1].hashCode())
                        {
                            case 1497762312: // triangle
                                currentFigure = new Triangle(myTexture,null);
                                break;
                            case 3481927: // quad
                                currentFigure = new Quad(myTexture, null);
                        }
                        break;
                    case 3521: // no

                        break;
                    case 3542: // oe
                        currentFigure.genObject();
                        theObject.addFigure(currentFigure);
                        break;
                    case 116893: // vmt
                        int currentID = Integer.parseInt(currentCommands[1]);
                        float x,y,z,u,v;
                        x = Float.parseFloat(currentCommands[2]);
                        y = Float.parseFloat(currentCommands[3]);
                        z = Float.parseFloat(currentCommands[4]);
                        u = Float.parseFloat(currentCommands[5]);
                        v = Float.parseFloat(currentCommands[6]);
                        Vertex myVertex = new Vertex(new Vector3f(x,y,z), new Vector2f(u,v));
                        currentFigure.setVertex(currentID, myVertex);
                        break;
                    case 118: // v

                        break;
                    case 3757: // vc

                        break;
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    /**
     * Get the WorldObject where the model is drawn in.
     * @return The WorldObject where the model is drawn in.
     */
    public SimpleObject getObject()
    {
        return theObject;
    }
}
